const express = require("express");
const router = express.Router();
const url = require("url")
const SQLConnect = require("./SQLConnect.js")
const jwt = require("jsonwebtoken")
const jwtSecret = require("./jwtSecret")
const adminData = require("./data/admin.js")
const vipData = require("./data/vip.js")
const lineData = require("./data/line.js")
const multer = require("multer")
const fs = require("fs")

/**
 * @swagger
 * /login:
 *   post:
 *     summary: 用户登录
 *     description: 用户通过用户名和密码进行登录认证
 *     tags: [用户认证]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - username
 *               - password
 *             properties:
 *               username:
 *                 type: string
 *                 description: 用户名
 *                 example: "admin"
 *               password:
 *                 type: string
 *                 description: 密码
 *                 example: "123456"
 *     responses:
 *       200:
 *         description: 登录成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/LoginResponse'
 *       500:
 *         description: 登录失败
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.post("/login", (req, res) => {
    // 接受客户端的参数：username， password
    const { username, password } = req.body;
    const sql = "select * from user where username=? and password=?";
    SQLConnect(sql, [username, password], result => {
        if (result.length > 0) {
            /**
             * 生成token
             * token:前后端在登录信息交互的时候，通过token验证是否登录成功的一个字段
             * token参考文档：http://www.ruanyifeng.com/blog/2018/07/json_web_token-tutorial.html
             */
            const token = jwt.sign({
                id: result[0].id,
                username: result[0].username,
                permission: result[0].permission
            }, jwtSecret.secret)
            res.send({
                status: 200,
                username: result[0].username,
                permission: result[0].permission,
                token
            })
        } else {
            res.send({
                status: 500,
                msg: "用户名密码错误"
            })
        }
    })
})

/**
 * @swagger
 * /router:
 *   get:
 *     summary: 获取用户权限菜单
 *     description: 根据用户权限获取对应的菜单数据
 *     tags: [权限管理]
 *     parameters:
 *       - in: query
 *         name: user
 *         schema:
 *           type: string
 *           enum: [admin, vip]
 *         description: 用户权限类型
 *         example: admin
 *     responses:
 *       200:
 *         description: 成功获取菜单数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 menuData:
 *                   type: array
 *                   description: 菜单数据
 */
router.get("/router", (req, res) => {
    const user = url.parse(req.url, true).query.user;
    switch (user) {
        case "admin":
            res.send({
                status: 200,
                menuData: adminData
            })
            break;
        case "vip":
            res.send({
                status: 200,
                menuData: vipData
            })
            break;
        default:
            res.send({
                status: 200,
                menuData: vipData
            })
            break;
    }
})

/**
 * @swagger
 * /line:
 *   get:
 *     summary: 获取线路数据
 *     description: 获取系统中的线路信息
 *     tags: [数据管理]
 *     responses:
 *       200:
 *         description: 成功获取线路数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 result:
 *                   type: array
 *                   description: 线路数据数组
 */
router.get("/line", (req, res) => {
    res.send({
        status: 200,
        result: lineData
    })
})

/**
 * @swagger
 * /project/all:
 *   get:
 *     summary: 获取所有项目信息
 *     description: 分页获取隧道项目信息
 *     tags: [项目管理]
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *         description: 页码
 *         example: 1
 *     responses:
 *       200:
 *         description: 成功获取项目数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 result:
 *                   type: array
 *                   description: 项目数据数组
 *       500:
 *         description: 暂无信息
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/project/all", (req, res) => {
    // 分页
    var page = url.parse(req.url, true).query.page || 1;
    const sql = "select * from project order by id desc limit 15 offset " + (page - 1) * 15;
    SQLConnect(sql, null, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                result
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无信息"
            })
        }
    })
})

/**
 * @swagger
 * /project/search:
 *   get:
 *     summary: 隧道项目模糊查询
 *     description: 根据关键词模糊查询隧道项目信息
 *     tags: [项目管理]
 *     parameters:
 *       - in: query
 *         name: search
 *         required: true
 *         schema:
 *           type: string
 *         description: 搜索关键词，可搜索项目名称、地址、备注等
 *         example: "隧道"
 *     responses:
 *       200:
 *         description: 查询成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 result:
 *                   type: array
 *                   description: 查询结果数组
 *       500:
 *         description: 暂无数据
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/project/search", (req, res) => {
    // 接受参数：查询内容
    const search = url.parse(req.url, true).query.search;
    // 模糊查询sql语句编写
    const sql = "select * from project where concat(`name`,`address`,`remark`) like '%" + search + "%'";
    SQLConnect(sql, null, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                result
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})

/**
 * @swagger
 * /project/total:
 *   get:
 *     summary: 获取项目总页数
 *     description: 获取隧道项目的总页数信息（分页计算）
 *     tags: [项目管理]
 *     responses:
 *       200:
 *         description: 成功获取总页数
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 result:
 *                   type: array
 *                   description: 包含总数的结果数组
 *       500:
 *         description: 暂无数据
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/project/total", (req, res) => {
    const sql = "select count(*) from project where id";
    SQLConnect(sql, null, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                result
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})

/**
 * @swagger
 * /project/add:
 *   get:
 *     summary: 添加隧道项目
 *     description: 添加新的隧道项目信息
 *     tags: [项目管理]
 *     parameters:
 *       - in: query
 *         name: name
 *         schema:
 *           type: string
 *         description: 项目名称
 *         example: "某某隧道工程"
 *       - in: query
 *         name: number
 *         schema:
 *           type: string
 *         description: 项目编号
 *         example: "TD001"
 *       - in: query
 *         name: money
 *         schema:
 *           type: string
 *         description: 项目金额
 *         example: "1000000"
 *       - in: query
 *         name: address
 *         schema:
 *           type: string
 *         description: 项目地址
 *         example: "北京市朝阳区"
 *       - in: query
 *         name: duration
 *         schema:
 *           type: string
 *         description: 工期
 *         example: "12个月"
 *       - in: query
 *         name: startTime
 *         schema:
 *           type: string
 *         description: 开始时间
 *         example: "2024-01-01"
 *       - in: query
 *         name: endTime
 *         schema:
 *           type: string
 *         description: 结束时间
 *         example: "2024-12-31"
 *       - in: query
 *         name: quantity
 *         schema:
 *           type: string
 *         description: 工程量
 *         example: "1000米"
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *         description: 项目状态
 *         example: "进行中"
 *       - in: query
 *         name: remark
 *         schema:
 *           type: string
 *         description: 备注
 *         example: "项目备注信息"
 *     responses:
 *       200:
 *         description: 添加成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 msg:
 *                   type: string
 *                   example: "添加成功"
 *       500:
 *         description: 添加失败
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/project/add", (req, res) => {
    var name = url.parse(req.url, true).query.name || "";
    var number = url.parse(req.url, true).query.number || "";
    var money = url.parse(req.url, true).query.money || "";
    var address = url.parse(req.url, true).query.address || "";
    var duration = url.parse(req.url, true).query.duration || "";
    var startTime = url.parse(req.url, true).query.startTime || "";
    var endTime = url.parse(req.url, true).query.endTime || "";
    var quantity = url.parse(req.url, true).query.quantity || "";
    var status = url.parse(req.url, true).query.status || "";
    var remark = url.parse(req.url, true).query.remark || "";
    const sql = "insert into project values (null,?,?,?,?,?,?,?,?,?,?)";
    const arr = [name, number, money, address, duration, startTime, endTime, quantity, status, remark]
    SQLConnect(sql, arr, result => {
        if (result.affectedRows > 0) {
            res.send({
                status: 200,
                msg: "添加成功"
            })
        } else {
            res.send({
                status: 500,
                msg: "添加失败"
            })
        }
    })
})

/**
 * @swagger
 * /project/del:
 *   get:
 *     summary: 删除隧道项目
 *     description: 根据项目ID删除隧道项目信息
 *     tags: [项目管理]
 *     parameters:
 *       - in: query
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 项目ID
 *         example: "1"
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 msg:
 *                   type: string
 *                   example: "删除成功"
 *       500:
 *         description: 删除失败
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/project/del", (req, res) => {
    var id = url.parse(req.url, true).query.id;
    var sql = "delete from project where id=?";
    SQLConnect(sql, [id], result => {
        if (result.affectedRows > 0) {
            res.send({
                status: 200,
                msg: "删除成功"
            })
        } else {
            res.send({
                status: 500,
                msg: "删除失败"
            })
        }
    })
})

/**
 * @swagger
 * /project/update/pre:
 *   get:
 *     summary: 隧道项目预更新
 *     description: 获取指定项目的详细信息，用于更新前的数据预填充
 *     tags: [项目管理]
 *     parameters:
 *       - in: query
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 项目ID
 *         example: "1"
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 result:
 *                   type: object
 *                   description: 项目详细信息
 *       500:
 *         description: 预更新失败
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/project/update/pre", (req, res) => {
    const id = url.parse(req.url, true).query.id;
    const sql = "select * from project where id=?";
    SQLConnect(sql, [id], result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                result: result[0]
            })
        } else {
            res.send({
                status: 500,
                msg: "预更新失败"
            })
        }
    })
})

/**
 * @swagger
 * /project/update/{id}:
 *   put:
 *     summary: 修改隧道项目
 *     description: 通过项目ID修改隧道项目的详细信息
 *     tags: [项目管理]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 项目ID
 *         example: "1"
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - number
 *               - money
 *               - address
 *               - duration
 *               - startTime
 *               - endTime
 *               - quantity
 *               - status
 *               - remark
 *             properties:
 *               name:
 *                 type: string
 *                 description: 项目名称
 *                 example: "某某隧道工程"
 *               number:
 *                 type: string
 *                 description: 项目编号
 *                 example: "TD001"
 *               money:
 *                 type: string
 *                 description: 项目金额
 *                 example: "1000000"
 *               address:
 *                 type: string
 *                 description: 项目地址
 *                 example: "北京市朝阳区"
 *               duration:
 *                 type: string
 *                 description: 工期
 *                 example: "12个月"
 *               startTime:
 *                 type: string
 *                 description: 开始时间
 *                 example: "2024-01-01"
 *               endTime:
 *                 type: string
 *                 description: 结束时间
 *                 example: "2024-12-31"
 *               quantity:
 *                 type: string
 *                 description: 工程量
 *                 example: "1000米"
 *               status:
 *                 type: string
 *                 description: 项目状态
 *                 example: "进行中"
 *               remark:
 *                 type: string
 *                 description: 备注
 *                 example: "项目备注信息"
 *     responses:
 *       200:
 *         description: 修改成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 msg:
 *                   type: string
 *                   example: "修改成功"
 *       500:
 *         description: 修改失败
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.put("/project/update/:id", (req, res) => {
    const id = req.params.id;
    const { name, number, money, address, duration, startTime, endTime, quantity, status, remark } = req.body;
    const sql = "update project set name=?,number=?,money=?,address=?,duration=?,startTime=?,endTime=?,quantity=?,status=?,remark=? where id=?";
    const arr = [name, number, money, address, duration, startTime, endTime, quantity, status, remark, id]
    SQLConnect(sql, arr, result => {
        if (result.affectedRows > 0) {
            res.send({
                status: 200,
                msg: "修改成功"
            })
        } else {
            res.send({
                status: 500,
                msg: "修改失败"
            })
        }
    })
})

/**
 * @swagger
 * /tunnel/list:
 *   get:
 *     summary: 获取隧道设计信息树状列表（一级）
 *     description: 获取隧道设计信息的一级分类列表
 *     tags: [隧道设计]
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 result:
 *                   type: array
 *                   description: 隧道一级分类数据
 *       500:
 *         description: 暂无数据
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/tunnel/list", (req, res) => {
    const sql = "select * from tunnel"
    SQLConnect(sql, null, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                result
            })
        } else {
            res.send({
                status: 500,
                msg: '暂无数据'
            })
        }
    })
})

/**
 * @swagger
 * /tunnel/list/child:
 *   get:
 *     summary: 获取隧道设计信息树状列表（二级）
 *     description: 根据一级分类ID获取对应的二级分类列表
 *     tags: [隧道设计]
 *     parameters:
 *       - in: query
 *         name: cid
 *         required: true
 *         schema:
 *           type: string
 *         description: 一级分类ID
 *         example: "1"
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 result:
 *                   type: array
 *                   description: 隧道二级分类数据
 *       500:
 *         description: 暂无数据
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/tunnel/list/child", (req, res) => {
    const cid = url.parse(req.url, true).query.cid
    const sql = "select * from tunnelchild where cid=?"
    SQLConnect(sql, [cid], result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                result
            })
        } else {
            res.send({
                status: 500,
                msg: '暂无数据'
            })
        }
    })
})

/**
 * @swagger
 * /tunnel/content:
 *   get:
 *     summary: 获取隧道设计信息内容
 *     description: 根据内容标识获取隧道设计的具体内容信息
 *     tags: [隧道设计]
 *     parameters:
 *       - in: query
 *         name: content
 *         required: true
 *         schema:
 *           type: string
 *         description: 内容标识
 *         example: "design001"
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 result:
 *                   type: array
 *                   description: 隧道设计内容数据
 *       500:
 *         description: 暂无数据
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/tunnel/content", (req, res) => {
    const content = url.parse(req.url, true).query.content;
    const sql = "select * from tunnelcontent where content=?"
    SQLConnect(sql, [content], result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                result
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})

/**
 * @swagger
 * /upload:
 *   post:
 *     summary: 文件上传
 *     description: 上传文件到服务器
 *     tags: [文件管理]
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               file:
 *                 type: string
 *                 format: binary
 *                 description: 要上传的文件
 *     responses:
 *       200:
 *         description: 上传成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 res_code:
 *                   type: string
 *                   example: "0"
 *                 name:
 *                   type: string
 *                   description: 原始文件名
 *                   example: "document.pdf"
 *                 url:
 *                   type: string
 *                   description: 文件保存路径
 *                   example: "./upload/1669364837999-document.pdf"
 */
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, "./upload/")
    },
    filename: function (req, file, cb) {
        cb(null, Date.now() + "-" + file.originalname)
    }
})

var createFolder = function (folder) {
    try {
        fs.accessSync(folder);
    } catch (e) {
        fs.mkdirSync(folder);
    }
}

var uploadFolder = './upload/';
createFolder(uploadFolder);
var upload = multer({ storage: storage });

router.post('/upload', upload.single('file'), function (req, res, next) {
    var file = req.file;
    console.log('文件类型：%s', file.mimetype);
    console.log('原始文件名：%s', file.originalname);
    console.log('文件大小：%s', file.size);
    console.log('文件保存路径：%s', file.path);
    res.json({ res_code: '0', name: file.originalname, url: file.path });
});

/**
 * @swagger
 * /tunnel/content/url:
 *   get:
 *     summary: 更新隧道设计信息文件路径
 *     description: 更新隧道设计内容的文件URL路径
 *     tags: [隧道设计]
 *     parameters:
 *       - in: query
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 内容ID
 *         example: "1"
 *       - in: query
 *         name: urlName
 *         required: true
 *         schema:
 *           type: string
 *         description: 文件URL路径
 *         example: "./upload/1669364837999-document.pdf"
 *     responses:
 *       200:
 *         description: 上传成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 msg:
 *                   type: string
 *                   example: "上传成功"
 *       500:
 *         description: 上传失败
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/tunnel/content/url",(req,res) =>{
    // id  url
    const id = url.parse(req.url, true).query.id;
    const urlName = url.parse(req.url, true).query.urlName;
    const sql = "update tunnelcontent set urlName=? where id=?"
    SQLConnect(sql,[urlName,id],result =>{
        if (result.affectedRows > 0) {
            res.send({
                status: 200,
                msg: "上传成功"
            })
        } else {
            res.send({
                status: 500,
                msg: "上传失败"
            })
        }
    })
})

/**
 * @swagger
 * /tunnel/pdf:
 *   get:
 *     summary: PDF文件预览
 *     description: 获取指定隧道设计内容的PDF文件信息用于预览
 *     tags: [隧道设计]
 *     parameters:
 *       - in: query
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 内容ID
 *         example: "1"
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 result:
 *                   type: object
 *                   description: PDF文件信息
 *       500:
 *         description: 暂无数据
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/tunnel/pdf",(req,res) =>{
    const id = url.parse(req.url, true).query.id;
    const sql = "select * from tunnelcontent where id=?"
    SQLConnect(sql,[id],result =>{
        if(result.length > 0){
            res.send({
                status:200,
                result:result[0]
            })
        }else{
            res.send({
                status:500,
                msg:"暂无数据"
            })
        }
    })
})

/**
 * @swagger
 * /user/list:
 *   get:
 *     summary: 获取用户列表
 *     description: 获取系统中所有用户的列表信息
 *     tags: [用户管理]
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 result:
 *                   type: array
 *                   description: 用户列表数据
 *       500:
 *         description: 暂无数据
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/user/list",(req,res) =>{
    const sql = "select * from user"
    SQLConnect(sql,null,result =>{
        if(result.length > 0){
            res.send({
                status:200,
                result
            })
        }else{
            res.send({
                status:500,
                msg:"暂无数据"
            })
        }
    })
})

/**
 * @swagger
 * /user/search:
 *   get:
 *     summary: 用户搜索
 *     description: 根据关键词模糊搜索用户信息
 *     tags: [用户管理]
 *     parameters:
 *       - in: query
 *         name: search
 *         required: true
 *         schema:
 *           type: string
 *         description: 搜索关键词，可搜索用户名、权限、手机号
 *         example: "admin"
 *     responses:
 *       200:
 *         description: 搜索成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 result:
 *                   type: array
 *                   description: 搜索结果数据
 *       500:
 *         description: 暂无数据
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/user/search",(req,res) =>{
    const search = url.parse(req.url,true).query.search;
    const sql = "select * from user where concat(`username`,`permission`,`phone`) like '%" + search + "%'";
    SQLConnect(sql,null,result =>{
        if(result.length > 0){
            res.send({
                status:200,
                result
            })
        }else{
            res.send({
                status:500,
                msg:"暂无数据"
            })
        }
    })
})

/**
 * @swagger
 * /user/add:
 *   get:
 *     summary: 添加用户
 *     description: 添加新的系统用户
 *     tags: [用户管理]
 *     parameters:
 *       - in: query
 *         name: username
 *         required: true
 *         schema:
 *           type: string
 *         description: 用户名
 *         example: "newuser"
 *       - in: query
 *         name: password
 *         required: true
 *         schema:
 *           type: string
 *         description: 密码
 *         example: "123456"
 *       - in: query
 *         name: permission
 *         required: true
 *         schema:
 *           type: string
 *           enum: [admin, vip]
 *         description: 用户权限
 *         example: "vip"
 *       - in: query
 *         name: phone
 *         required: true
 *         schema:
 *           type: string
 *         description: 手机号码
 *         example: "13800138000"
 *     responses:
 *       200:
 *         description: 添加成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 msg:
 *                   type: string
 *                   example: "添加成功"
 *       500:
 *         description: 添加失败
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/user/add",(req,res) =>{
    const username = url.parse(req.url,true).query.username;
    const password = url.parse(req.url,true).query.password;
    const permission = url.parse(req.url,true).query.permission;
    const phone = url.parse(req.url,true).query.phone;
    const sql = "insert into user values (null,?,?,?,?)"
    SQLConnect(sql,[username,password,permission,phone],result =>{
        if (result.affectedRows > 0) {
            res.send({
                status: 200,
                msg: "添加成功"
            })
        } else {
            res.send({
                status: 500,
                msg: "添加失败"
            })
        }
    })
})

/**
 * @swagger
 * /user/del:
 *   get:
 *     summary: 删除用户
 *     description: 根据用户ID删除用户（不能删除ID为1的用户）
 *     tags: [用户管理]
 *     parameters:
 *       - in: query
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 用户ID（不能为1）
 *         example: "2"
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 msg:
 *                   type: string
 *                   example: "删除成功"
 *       500:
 *         description: 删除失败
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/user/del",(req,res) =>{
    const id = url.parse(req.url,true).query.id;
    if(id == 1)return;
    const sql = "delete from user where id=?";
    SQLConnect(sql,[id],result =>{
        if(result.affectedRows > 0){
            res.send({
                status:200,
                msg:"删除成功"
            })
        }else{
            res.send({
                status:500,
                msg:"删除失败"
            })
        }
    })
})

/**
 * @swagger
 * /user/preview:
 *   get:
 *     summary: 用户预更新
 *     description: 获取指定用户的详细信息，用于更新前的数据预填充
 *     tags: [用户管理]
 *     parameters:
 *       - in: query
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 用户ID
 *         example: "1"
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 result:
 *                   type: array
 *                   description: 用户详细信息
 *       500:
 *         description: 暂无数据
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/user/preview",(req,res) =>{
    const id = url.parse(req.url,true).query.id;
    const sql = "select * from user where id=?";
    SQLConnect(sql,[id],result =>{
        if(result.length > 0){
            res.send({
                status:200,
                result
            })
        }else{
            res.send({
                status:500,
                msg:"暂无数据"
            })
        }
    })
})

/**
 * @swagger
 * /user/update:
 *   get:
 *     summary: 修改用户信息
 *     description: 根据用户ID修改用户的密码、权限和手机号
 *     tags: [用户管理]
 *     parameters:
 *       - in: query
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 用户ID
 *         example: "1"
 *       - in: query
 *         name: password
 *         required: true
 *         schema:
 *           type: string
 *         description: 新密码
 *         example: "newpassword"
 *       - in: query
 *         name: permission
 *         required: true
 *         schema:
 *           type: string
 *           enum: [admin, vip]
 *         description: 用户权限
 *         example: "admin"
 *       - in: query
 *         name: phone
 *         required: true
 *         schema:
 *           type: string
 *         description: 手机号码
 *         example: "13800138000"
 *     responses:
 *       200:
 *         description: 修改成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: number
 *                   example: 200
 *                 msg:
 *                   type: string
 *                   example: "修改成功"
 *       500:
 *         description: 修改失败
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get("/user/update",(req,res) =>{
    const id = url.parse(req.url,true).query.id;
    const password = url.parse(req.url,true).query.password;
    const permission = url.parse(req.url,true).query.permission;
    const phone = url.parse(req.url,true).query.phone;
    const sql = "update user set password=?,permission=?,phone=? where id=?"
    SQLConnect(sql,[password,permission,phone,id],result =>{
        if(result.affectedRows > 0){
            res.send({
                status:200,
                msg:"修改成功"
            })
        }else{
            res.send({
                status:500,
                msg:"修改失败"
            })
        }
    })
})

module.exports = router;